package com.dodock.survey.ui;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TimePicker;

import com.dodock.survey.Brave;
import com.dodock.survey.R;
import com.dodock.survey.adapter.NoDefaultSpinner;
import com.dodock.survey.baseclasses.Information;

public class InformationActivity extends Activity implements OnClickListener{

	EditText infoAorB;
	EditText facilityCode;
	EditText subjectId;
	EditText subjectName;
	EditText infoAddOfSubj1;
	EditText infoNameOfHospital;
	EditText infoWordNoOrOPD;
	EditText infoHospitalRegNo;
	EditText info1stInterviewerNameOrCode;
	EditText info2ndInterviewerNameOrCode;	
	CheckBox infoUsr1sectionFilled1;
	CheckBox infoUsr1sectionFilled2;
	CheckBox infoUsr1sectionFilled3;
	CheckBox infoUsr1sectionFilled4;
	CheckBox infoUsr1sectionFilled5;
	CheckBox infoUsr1sectionFilled6;
	CheckBox infoUsr1sectionFilled7;
	CheckBox infoUsr1sectionFilled8;
	CheckBox infoUsr1sectionFilled9;
	CheckBox infoUsr1sectionFilled10;
	CheckBox infoUsr1sectionFilled11;
	CheckBox infoUsr1sectionFilled12;
	CheckBox infoUsr1sectionFilled13;
	CheckBox infoUsr1sectionFilled14;
	CheckBox infoUsr1sectionFilled15;
	
	CheckBox infoUsr2sectionFilled1;
	CheckBox infoUsr2sectionFilled2;
	CheckBox infoUsr2sectionFilled3;
	CheckBox infoUsr2sectionFilled4;
	CheckBox infoUsr2sectionFilled5;
	CheckBox infoUsr2sectionFilled6;
	CheckBox infoUsr2sectionFilled7;
	CheckBox infoUsr2sectionFilled8;
	CheckBox infoUsr2sectionFilled9;
	CheckBox infoUsr2sectionFilled10;
	CheckBox infoUsr2sectionFilled11;
	CheckBox infoUsr2sectionFilled12;
	CheckBox infoUsr2sectionFilled13;
	CheckBox infoUsr2sectionFilled14;
	CheckBox infoUsr2sectionFilled15;
	
	RadioButton infoDataColCompleteRB;
	RadioButton infoDataColIncompleteRB;

	NoDefaultSpinner infoReasonForIncomplete;
	Button infoDateOfEnrolement;
	Button infoDateOfInterview;
	Button infoDateOfSampling;
	Button infoDateOfLastMeal;
	Button infoEndDate;

	private Button infoEndTime;	
	private Button infoStartTime;
	private Button infoTimeOfSampling;
	private Button infoTimeOfLastMeal;

	boolean newForm;
	private int year;
	private int month;
	private int day;
	
	int currentButtonId = 0;
	int hour;
	int minute;
	
	final int DATE_DIALOG_ID = 998;
	final int TIME_DIALOG_ID = 999;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		newForm = getIntent().getExtras().getBoolean("newForm");
		int itemNo = getIntent().getExtras().getInt("itemNo");
				
		setContentView(R.layout.information_layout);

		initView();
		
		Button okBtn = (Button) findViewById(R.id.okBtn);
		okBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				
				storeAndGoIntoSection();
			}
		});
		
		if(!newForm){
			Brave.USER_NAME = Brave.infoList.get(itemNo).getKey();
			restoreDataIntoView();
		}else{
			Brave.USER_NAME = String.valueOf(System.currentTimeMillis());
		}
	}
	
	private void openErrorDialog(String title, String err_msg){
		
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);
		alert.setTitle(title);
		alert.setMessage(err_msg);
		alert.setCancelable(true);

		alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				
			}
		});

		alert.show();
	}
	
	private void initView(){
		infoAorB = (EditText) findViewById(R.id.infoAorB);
		facilityCode = (EditText) findViewById(R.id.facilityCode);
		subjectId = (EditText) findViewById(R.id.subjectId);
		subjectName = (EditText) findViewById(R.id.subjectName);
		infoAddOfSubj1 = (EditText) findViewById(R.id.addressOfSubj);
		infoNameOfHospital = (EditText) findViewById(R.id.nameOfHospital);
		infoWordNoOrOPD = (EditText) findViewById(R.id.wordNoOpd);
		infoHospitalRegNo = (EditText) findViewById(R.id.hospitalRegNo);
		info1stInterviewerNameOrCode = (EditText) findViewById(R.id.firstInerviewNameCode);
		info2ndInterviewerNameOrCode = (EditText) findViewById(R.id.secondInterviewerNameCode);
		infoUsr1sectionFilled1 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled1);
		infoUsr1sectionFilled2 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled2);
		infoUsr1sectionFilled3 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled3);
		infoUsr1sectionFilled4 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled4);
		infoUsr1sectionFilled5 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled5);
		infoUsr1sectionFilled6 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled6);
		infoUsr1sectionFilled7 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled7);
		infoUsr1sectionFilled8 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled8);		
		infoUsr1sectionFilled9 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled9);
		infoUsr1sectionFilled10 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled10);
		infoUsr1sectionFilled11 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled11);
		infoUsr1sectionFilled12 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled12);
		infoUsr1sectionFilled13 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled13);
		infoUsr1sectionFilled14 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled14);
		infoUsr1sectionFilled15 = (CheckBox) findViewById(R.id.infoUsr1sectionFilled15);
		infoUsr2sectionFilled1 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled1);
		infoUsr2sectionFilled2 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled2);
		infoUsr2sectionFilled3 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled3);
		infoUsr2sectionFilled4 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled4);
		infoUsr2sectionFilled5 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled5);
		infoUsr2sectionFilled6 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled6);
		infoUsr2sectionFilled7 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled7);
		infoUsr2sectionFilled8 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled8);		
		infoUsr2sectionFilled9 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled9);
		infoUsr2sectionFilled10 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled10);
		infoUsr2sectionFilled11 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled11);
		infoUsr2sectionFilled12 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled12);
		infoUsr2sectionFilled13 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled13);
		infoUsr2sectionFilled14 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled14);
		infoUsr2sectionFilled15 = (CheckBox) findViewById(R.id.infoUsr2sectionFilled15);

		infoDataColCompleteRB = (RadioButton) findViewById(R.id.infoDataColCompleteRB);
		infoDataColIncompleteRB = (RadioButton) findViewById(R.id.infoDataColIncompleteRB);
		
		infoReasonForIncomplete = (NoDefaultSpinner) findViewById(R.id.infoReasonForIncomplete);
		final Calendar c = Calendar.getInstance();
		year = c.get(Calendar.YEAR);
		month = c.get(Calendar.MONTH);
		day = c.get(Calendar.DAY_OF_MONTH);
		hour = c.get(Calendar.HOUR_OF_DAY);
		minute = c.get(Calendar.MINUTE);
		
		
		infoDateOfEnrolement = (Button) findViewById(R.id.dateOfEnrolmentButton);
		infoDateOfEnrolement.setOnClickListener(this);
		infoDateOfEnrolement.setText(day+"-"+(month + 1)+"-"+year);
		
		infoDateOfInterview = (Button) findViewById(R.id.dateOfInterview);
		infoDateOfInterview.setOnClickListener(this);
		infoDateOfInterview.setText(day+"-"+(month + 1)+"-"+year);
		
		infoDateOfSampling = (Button) findViewById(R.id.dateOfSampling);
		infoDateOfSampling.setOnClickListener(this);
		infoDateOfSampling.setText(day+"-"+(month + 1)+"-"+year);
		
		infoDateOfLastMeal = (Button) findViewById(R.id.dateOfLastMeal);
		infoDateOfLastMeal.setOnClickListener(this);
		infoDateOfLastMeal.setText(day+"-"+(month + 1)+"-"+year);
		
		infoEndDate = (Button) findViewById(R.id.endDate);
		infoEndDate.setOnClickListener(this);
		infoEndDate.setText( day + "-" + (month + 1) + "-" + year );
		
		infoEndTime = (Button) findViewById(R.id.endTime);
		infoEndTime.setOnClickListener(this);
		infoEndTime.setText( hour + ":" + minute );
		
		infoStartTime = (Button) findViewById(R.id.startTime);
		infoStartTime.setOnClickListener(this);
		infoStartTime.setText( hour + ":" +minute );
		
		infoTimeOfSampling = (Button) findViewById(R.id.timeOfSampling);
		infoTimeOfSampling.setText( hour + ":" + minute );
		infoTimeOfSampling.setOnClickListener(this);
		
		infoTimeOfLastMeal= (Button) findViewById(R.id.timeOfLastMeal);
		infoTimeOfLastMeal.setOnClickListener(this);
		infoTimeOfLastMeal.setText( hour + ":" + minute );
	}

	private void restoreDataIntoView(){
		Information information = Brave.getDatabaseManager().getInformation(Brave.USER_NAME);
		if(information!=null){
			infoAorB.setText(information.getInfoId());
			facilityCode.setText(information.getInfoFacilityCode());
			subjectId.setText(information.getInfoSubjectId());
			subjectName.setText(information.getInfoSubjectName());
			infoAddOfSubj1.setText(information.getInfoAddressAndContactNumbOfSubj());
			infoNameOfHospital.setText(information.getInfoNameOfHospital());
			infoWordNoOrOPD.setText(information.getInfoWordNoOrOPD());
			infoHospitalRegNo.setText(information.getInfoHospitalRegNo());
			info1stInterviewerNameOrCode.setText(information.getInfoInterviewerNameOrCode());
			info2ndInterviewerNameOrCode.setText(information.getInfoSecondInterviewerNameOrCode());
			infoUsr1sectionFilled1.setChecked(information.getInfoUsr1sectionFilled1() == 1 ? true : false);
			infoUsr1sectionFilled2.setChecked(information.getInfoUsr1sectionFilled2() == 1 ? true : false);
			infoUsr1sectionFilled3.setChecked(information.getInfoUsr1sectionFilled3() == 1 ? true : false);
			infoUsr1sectionFilled4.setChecked(information.getInfoUsr1sectionFilled4() == 1 ? true : false);
			infoUsr1sectionFilled5.setChecked(information.getInfoUsr1sectionFilled5() == 1 ? true : false);
			infoUsr1sectionFilled6.setChecked(information.getInfoUsr1sectionFilled6() == 1 ? true : false);
			infoUsr1sectionFilled7.setChecked(information.getInfoUsr1sectionFilled7() == 1 ? true : false);
			infoUsr1sectionFilled8.setChecked(information.getInfoUsr1sectionFilled8() == 1 ? true : false);
			infoUsr1sectionFilled9.setChecked(information.getInfoUsr1sectionFilled9() == 1 ? true : false);
			infoUsr1sectionFilled10.setChecked(information.getInfoUsr1sectionFilled10() == 1 ? true : false);
			infoUsr1sectionFilled11.setChecked(information.getInfoUsr1sectionFilled11() == 1 ? true : false);
			infoUsr1sectionFilled12.setChecked(information.getInfoUsr1sectionFilled12() == 1 ? true : false);
			infoUsr1sectionFilled13.setChecked(information.getInfoUsr1sectionFilled13() == 1 ? true : false);
			infoUsr1sectionFilled14.setChecked(information.getInfoUsr1sectionFilled14() == 1 ? true : false);
			infoUsr1sectionFilled15.setChecked(information.getInfoUsr1sectionFilled15() == 1 ? true : false);
			infoUsr2sectionFilled1.setChecked(information.getInfoUsr2sectionFilled1() == 1 ? true : false);
			infoUsr2sectionFilled2.setChecked(information.getInfoUsr2sectionFilled2() == 1 ? true : false);
			infoUsr2sectionFilled3.setChecked(information.getInfoUsr2sectionFilled3() == 1 ? true : false);
			infoUsr2sectionFilled4.setChecked(information.getInfoUsr2sectionFilled4() == 1 ? true : false);
			infoUsr2sectionFilled5.setChecked(information.getInfoUsr2sectionFilled5() == 1 ? true : false);
			infoUsr2sectionFilled6.setChecked(information.getInfoUsr2sectionFilled6() == 1 ? true : false);
			infoUsr2sectionFilled7.setChecked(information.getInfoUsr2sectionFilled7() == 1 ? true : false);
			infoUsr2sectionFilled8.setChecked(information.getInfoUsr2sectionFilled8() == 1 ? true : false);
			infoUsr2sectionFilled9.setChecked(information.getInfoUsr2sectionFilled9() == 1 ? true : false);
			infoUsr2sectionFilled10.setChecked(information.getInfoUsr2sectionFilled10() == 1 ? true : false);
			infoUsr2sectionFilled11.setChecked(information.getInfoUsr2sectionFilled11() == 1 ? true : false);
			infoUsr2sectionFilled12.setChecked(information.getInfoUsr2sectionFilled12() == 1 ? true : false);
			infoUsr2sectionFilled13.setChecked(information.getInfoUsr2sectionFilled13() == 1 ? true : false);
			infoUsr2sectionFilled14.setChecked(information.getInfoUsr2sectionFilled14() == 1 ? true : false);
			infoUsr2sectionFilled15.setChecked(information.getInfoUsr2sectionFilled15() == 1 ? true : false);
			if(information.getInfoDataCollectionStatus() == 1){
				infoDataColCompleteRB.setChecked(true);
			}else if(information.getInfoDataCollectionStatus() == 2){
				infoDataColIncompleteRB.setChecked(true);
			}
			infoReasonForIncomplete.setSelection(information.getInfoReasonForIncomplete());
			infoDateOfEnrolement.setText(information.getInfoDateOfEnrolement());
			infoDateOfInterview.setText(information.getInfoDateOfInterview());
			infoDateOfSampling.setText(information.getInfoDateOfSampling());
			infoDateOfLastMeal.setText(information.getInfoDateOfLastMeal());
			infoEndDate.setText(information.getInfoEndDate());
			infoEndTime.setText(information.getInfoEndTime());
			infoStartTime.setText(information.getInfoStartTime());
			infoTimeOfSampling.setText(information.getInfoTimeOfSampling());
			infoTimeOfLastMeal.setText(information.getInfoTimeOfLastMeal());
		}
	}
	
	

	private void storeAndGoIntoSection(){	
		
		if(infoAorB.getText().toString().equals("A") || infoAorB.getText().toString().equals("B")||
				infoAorB.getText().toString().equals("")){
			Brave.ID_value = infoAorB.getText().toString();
		}else{
			openErrorDialog("Wrong id","Please specify Id as A or B");
			infoAorB.getText().clear();
			infoAorB.requestFocus();
			return;
		}

		ContentValues cv = new ContentValues();
		cv.put("key", Brave.encryptString(Brave.USER_NAME));
		cv.put("infoId", Brave.encryptString(infoAorB.getText().toString()));
		cv.put("infoFacilityCode", Brave.encryptString(facilityCode.getText().toString()));
		cv.put("infoSubjectId", Brave.encryptString(subjectId.getText().toString()));
		cv.put("infoSubjectName", Brave.encryptString(subjectName.getText().toString()));
		cv.put("infoAddOfSubj1", Brave.encryptString(infoAddOfSubj1.getText().toString()));
		cv.put("infoNameOfHospital", Brave.encryptString(infoNameOfHospital.getText().toString()));
		cv.put("infoWordNoOrOPD", Brave.encryptString(infoWordNoOrOPD.getText().toString()));
		cv.put("infoHospitalRegNo", Brave.encryptString(infoHospitalRegNo.getText().toString()));
		cv.put("info1stInterviewerNameOrCode", Brave.encryptString(info1stInterviewerNameOrCode.getText().toString()));
		cv.put("info2ndInterviewerNameOrCode", Brave.encryptString(info2ndInterviewerNameOrCode.getText().toString()));
		cv.put("infoUsr1sectionFilled1", Brave.encryptInteger(infoUsr1sectionFilled1.isChecked() == true ? 1 : 0));
		cv.put("infoUsr1sectionFilled2", Brave.encryptInteger(infoUsr1sectionFilled2.isChecked() == true ? 1 : 0));
		cv.put("infoUsr1sectionFilled3", Brave.encryptInteger(infoUsr1sectionFilled3.isChecked() == true ? 1 : 0));
		cv.put("infoUsr1sectionFilled4", Brave.encryptInteger(infoUsr1sectionFilled4.isChecked() == true ? 1 : 0));
		cv.put("infoUsr1sectionFilled5", Brave.encryptInteger(infoUsr1sectionFilled5.isChecked() == true ? 1 : 0));
		cv.put("infoUsr1sectionFilled6", Brave.encryptInteger(infoUsr1sectionFilled6.isChecked() == true ? 1 : 0));
		cv.put("infoUsr1sectionFilled7", Brave.encryptInteger(infoUsr1sectionFilled7.isChecked() == true ? 1 : 0));
		cv.put("infoUsr1sectionFilled8", Brave.encryptInteger(infoUsr1sectionFilled8.isChecked() == true ? 1 : 0));
		cv.put("infoUsr1sectionFilled9", Brave.encryptInteger(infoUsr1sectionFilled9.isChecked() == true ? 1 : 0));
		cv.put("infoUsr1sectionFilled10", Brave.encryptInteger(infoUsr1sectionFilled10.isChecked() == true ? 1 : 0));
		cv.put("infoUsr1sectionFilled11", Brave.encryptInteger(infoUsr1sectionFilled11.isChecked() == true ? 1 : 0));
		cv.put("infoUsr1sectionFilled12", Brave.encryptInteger(infoUsr1sectionFilled12.isChecked() == true ? 1 : 0));
		cv.put("infoUsr1sectionFilled13", Brave.encryptInteger( infoUsr1sectionFilled13.isChecked() == true ? 1 : 0));
		cv.put("infoUsr1sectionFilled14", Brave.encryptInteger(infoUsr1sectionFilled14.isChecked() == true ? 1 : 0));
		cv.put("infoUsr1sectionFilled15", Brave.encryptInteger(infoUsr1sectionFilled15.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled1", Brave.encryptInteger(infoUsr2sectionFilled1.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled2", Brave.encryptInteger( infoUsr2sectionFilled2.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled3", Brave.encryptInteger(infoUsr2sectionFilled3.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled4", Brave.encryptInteger(infoUsr2sectionFilled4.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled5", Brave.encryptInteger(infoUsr2sectionFilled5.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled6", Brave.encryptInteger(infoUsr2sectionFilled6.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled7", Brave.encryptInteger(infoUsr2sectionFilled7.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled8", Brave.encryptInteger(infoUsr2sectionFilled8.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled9", Brave.encryptInteger(infoUsr2sectionFilled9.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled10", Brave.encryptInteger(infoUsr2sectionFilled10.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled11", Brave.encryptInteger(infoUsr2sectionFilled11.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled12", Brave.encryptInteger(infoUsr2sectionFilled12.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled13", Brave.encryptInteger(infoUsr2sectionFilled13.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled14", Brave.encryptInteger(infoUsr2sectionFilled14.isChecked() == true ? 1 : 0));
		cv.put("infoUsr2sectionFilled15", Brave.encryptInteger(infoUsr2sectionFilled15.isChecked() == true ? 1 : 0));
		int dataColStatus = 0;
		if(infoDataColCompleteRB.isChecked())
			dataColStatus = 1;
		else if(infoDataColIncompleteRB.isSelected())
			dataColStatus = 2;
		cv.put("infoDataCollectionStatus", Brave.encryptInteger(dataColStatus));
		cv.put("infoReasonForIncomplete", Brave.encryptInteger(infoReasonForIncomplete.getSelectedItemPosition()));
		cv.put("infoDateOfEnrolement", Brave.encryptString(infoDateOfEnrolement.getText().toString()));
		cv.put("infoDateOfInterview", Brave.encryptString(infoDateOfInterview.getText().toString()));
		cv.put("infoDateOfSampling", Brave.encryptString(infoDateOfSampling.getText().toString()));
		cv.put("infoDateOfLastMeal", Brave.encryptString(infoDateOfLastMeal.getText().toString()));
		cv.put("infoEndDate", Brave.encryptString(infoEndDate.getText().toString()));
		cv.put("infoEndTime", Brave.encryptString(infoEndTime.getText().toString()));
		cv.put("infoStartTime", Brave.encryptString(infoStartTime.getText().toString()));		
		cv.put("infoTimeOfSampling", Brave.encryptString(infoTimeOfSampling.getText().toString()));		
		cv.put("infoTimeOfLastMeal", Brave.encryptString(infoTimeOfLastMeal.getText().toString()));

		Brave.getDatabaseManager().addInformatonIntoDB(cv , Brave.USER_NAME);
		Intent intent = new Intent( new Intent( InformationActivity.this, SectionActivity.class));
		intent.putExtra("newForm", newForm);
		startActivity(intent);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		currentButtonId = v.getId(); 
		Button btn = (Button) findViewById(currentButtonId);
		String text = btn.getText().toString();
		switch (currentButtonId) {
		
		case R.id.dateOfEnrolmentButton:
			getCurrentDate(text);
			showDialog(DATE_DIALOG_ID);
			break;	
			
		case R.id.dateOfInterview:
			getCurrentDate(text);
			showDialog(DATE_DIALOG_ID);
			break;
			
		case R.id.dateOfSampling:
			getCurrentDate(text);
			showDialog(DATE_DIALOG_ID);
			break;
			
		case R.id.dateOfLastMeal:
			getCurrentDate(text);
			showDialog(DATE_DIALOG_ID);
			break;
			
		case R.id.endDate:
			getCurrentDate(text);
			showDialog(DATE_DIALOG_ID);
			break;
			
		case R.id.endTime:
			getCurrentTime(text);
			showDialog(TIME_DIALOG_ID);
			break;
		
		case R.id.startTime:
			getCurrentTime(text);
			showDialog(TIME_DIALOG_ID);
			break;
			
		case R.id.timeOfSampling:
			getCurrentTime(text);
			showDialog(TIME_DIALOG_ID);
			break;
			
		case R.id.timeOfLastMeal:
			getCurrentTime(text);
			showDialog(TIME_DIALOG_ID);
			break;
			
		default:
			break;
		}	
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DATE_DIALOG_ID:
			 return new DatePickerDialog(this, datePickerListener, currentYear, currentMonth, currentDay);
		case TIME_DIALOG_ID:
			 return new TimePickerDialog(this, timePickerListener, currentHour, currentMin, true);
		}
		return null;
	}
	int currentHour = 0;
	int currentMin = 0;
	int currentMonth = 0;
	int currentDay = 0;
	int currentYear = 0;
	
	void getCurrentTime( String time){		
		final String SPLIT_STR = ":";
		final String[] splitStr = time.split(SPLIT_STR);		
		currentHour = Integer.parseInt(splitStr[0]);
		currentMin = Integer.parseInt(splitStr[1]);
	}
	
	
	void getCurrentDate(String date){		
		final String SPLIT_STR = "-";
		final String[] splitStr = date.split(SPLIT_STR);			
		currentDay = Integer.parseInt(splitStr[0]);
		currentMonth = Integer.parseInt(splitStr[1]) - 1;		
		currentYear = Integer.parseInt(splitStr[2]);		
	}
	
	private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {

		// when dialog box is closed, below method will be called.
		public void onDateSet(DatePicker view, int selectedYear,
				int selectedMonth, int selectedDay) {

			String date = (selectedMonth + 1)+ "-" +selectedDay+ "-" +selectedYear;			
			Button button = (Button) findViewById(currentButtonId);
			button.setText(date);
			
		}
	};

	private TimePickerDialog.OnTimeSetListener timePickerListener = 
            new TimePickerDialog.OnTimeSetListener() {
		public void onTimeSet(TimePicker view, int selectedHour,
				int selectedMinute) {
			hour = selectedHour;
			minute = selectedMinute;
			String time = hour + ":" + minute;		
			Button button = (Button) findViewById(currentButtonId);
			button.setText(time);
		}
	};

}
